package com.liming.service.wx.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.cipher.Signer;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liming
 * @date 2025/4/3
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "wx.payment.jsapi")
public class WxJsapiConfig {

    /**
     * appid
     */
    public String appid;

    /**
     * 商户号
     */
    public String merchantId;

    /**
     * 商户API私钥路径
     */
    public String privateKeyPath;

    /**
     * 公钥路径
     */
    public String publicKeyPath;

    /**
     * 商户APIv3密钥公钥
     */
    public String publicKeyId;

    /**
     * 商户证书序列号
     */
    public String merchantSerialNumber;

    /**
     * 商户APIV3密钥
     */
    public String apiV3key;

    @Bean
    public Config wechatConfig() {
        return new RSAPublicKeyConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .publicKeyFromPath(publicKeyPath)
                .publicKeyId(publicKeyId)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3key)
                .build();
    }

    @Bean
    public Signer mySigner(Config wechatConfig) {
        return wechatConfig.createSigner();
    }

    @Bean
    public JsapiService wechatJsapiService(Config wechatConfig) {
        return new JsapiService.Builder().config(wechatConfig).build();
    }

    @Bean
    public RefundService wechatRefundService(Config wechatPayConfig) {
        return new RefundService.Builder().config(wechatPayConfig).build();
    }
}
